const express = require('express');
const db=require('../utils/db')
//创建一个路由控制器
const router=express.Router()

//某人写的这个模块的相关接口
router.post('/register',function(req,res){
    console.log(req.body.username,req.body.password)
    const sql1=`select * from user where username='${req.body.username}';`
    const sql2=`insert into user value(null,'${req.body.username}','${req.body.password}');`

    db.query(sql1,function(err,data){
        if(err){
            res.send({code:500,msg:err})
        }else{
            if(data.length){
                res.send({code:500,msg:'账号已经存在'})
            }else{
                db.query(sql2,function(err,data){
                    if(err){
                        res.send({code:500,msg:err})
                    }else{
                        res.send({code:200,msg:'注册成功'})
                    }
                })
            }
        }
    })
})

router.post('/login',function(req,res){
    const sql=`select * from user where username='${req.body.username}' and password='${req.body.password}';`
    
    db.query(sql,function(err,data){
        if(err){
            res.send({code:500,msg:err})
        }else{
            //sql正确 但是不一定有这个用户
            if(data.length){
                res.send({code:200,msg:'登录成功'})
            }else{
                res.send({code:200,msg:'账号或密码错误'})
            }
        }
    })
})

//导出路由
module.exports=router;